﻿using Autofac;
using Language.Resource;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System;

namespace VisionCloud.Utility
{
    public class StartupServer
    {
        /// <summary>
        /// 初始化依赖注入和多语言资源
        /// </summary>
        /// <param name="env">环境变量</param>
        /// <param name="serviceProvider">依赖注入服务</param>
        public static void InjectServiceProvider(IWebHostEnvironment env, IServiceProvider serviceProvider)
        {
            ContentRootPath = env.ContentRootPath;
            WebRootPath = env.WebRootPath;
            ServiceProvider = serviceProvider;
            CommonLocalizer = serviceProvider.GetService<IStringLocalizer<CommonResource>>();
            ControllerLocalizer = serviceProvider.GetService<IStringLocalizer<ControllerResource>>();
            ServiceLocalizer = serviceProvider.GetService<IStringLocalizer<ServiceResource>>();
            RepositoryLocalizer = serviceProvider.GetService<IStringLocalizer<RepositoryResource>>();
            SurfaceLocalizer = serviceProvider.GetService<IStringLocalizer<SurfaceResource>>();
        }

        /// <summary>
        /// Autofac IOC容器
        /// </summary>
        public static IContainer IocContainer { get; set; }

        /// <summary>
        /// 依赖注入服务
        /// </summary>
        public static IServiceProvider ServiceProvider { get; set; }

        /// <summary>
        /// 获取当前线程HttpContext
        /// </summary>
        public static HttpContext HttpContext => ServiceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;

        /// <summary>
        /// 应用程序运行目录
        /// </summary>
        public static string ContentRootPath { get; set; }

        /// <summary>
        /// web服务目录
        /// </summary>
        public static string WebRootPath { get; set; }

        /// <summary>
        /// 多语言公共资源
        /// </summary>
        public static IStringLocalizer CommonLocalizer { get; set; }

        /// <summary>
        /// 多语言Controller资源
        /// </summary>
        public static IStringLocalizer ControllerLocalizer { get; set; }

        /// <summary>
        /// 多语言Service资源
        /// </summary>
        public static IStringLocalizer ServiceLocalizer { get; set; }

        /// <summary>
        /// 多语言Repository资源
        /// </summary>
        public static IStringLocalizer RepositoryLocalizer { get; set; }

        /// <summary>
        /// 多语言Repository资源
        /// </summary>
        public static IStringLocalizer SurfaceLocalizer { get; set; }

    }

    public static class LocalizeHelper
    {
        /// <summary>
        /// ServiceResource多语言
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SLocalize(this string value)
        {
            return StartupServer.ServiceLocalizer[value].Value;
        }

        /// <summary>
        /// ServiceResource多语言
        /// </summary>
        /// <param name="value"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string SLocalize(this string value, params object[] param)
        {
            return string.Format(StartupServer.ServiceLocalizer[value].Value, param);
        }

        /// <summary>
        /// surfaceresource多语言
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SurfaceLocalizer(this string value)
        {
            return StartupServer.SurfaceLocalizer[value].Value;
        }

        /// <summary>
        /// surfaceresource多语言
        /// </summary>
        /// <param name="value"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string SurfaceLocalizer(this string value, params object[] param)
        {
            return string.Format(StartupServer.SurfaceLocalizer[value].Value, param);
        }
    }
}